<!--给编辑界面里有个列表界面，然后列表界面上又有新增，修改按钮-->
<template>
    <div class='input-button-select'>
        <div v-if="!showButton" style='color:red;font-weight:bold'>请先保存，在添加信息</div>
        <el-button type="primary" @click="openWins" size="small" v-if="showButton">{{label}}</el-button>

        
    </div>
</template>
<script>
export default {
    name:'input-button-edit',
    props:['exkey','extype','id','label','data','appid','dataField'],
    
    data(){
        return{
            showButton:true,
            commonEditData:{
                fieldPath:"",
                filename2:"",
                sId:"",
                param_key:"",
                appid:"",
                title:"",
                params:{},
                width:"95%",
                left:"2%",
                top:"5%",
                height:"height:80VH"
            },
            dialogVisible:false,
            company:localStorage.getItem("company"),
            curRow:null,
            curRows:null,
            dataNew:this.data,
            id_:"",
            model:"",
            exData:{}
        }
    },
    created(){
        this.commonEditData.title=this.label;
        for(let key in this.exkey){
            this.commonEditData[key]=this.exkey[key];
        }
       

        this.initBtn();
        console.log(this.commonEditData);


    },
    watch:{
        data:{
            handler: function (newVal) {
                // console.log("data===================================================")
                this.initBtn()
            },      
            deep: true    //深度监听
        }
    },
    methods:{
        initBtn(){
            if(this.exkey["type"]=="add"||this.exkey["type"]=="edit"||this.exkey["type"]=="del"){
                let sId=this.exkey["sId"];
                let mainId=this.exkey["mainId"];
                let pre=this.exkey["pre"];
                let relMainId=this.exkey["relMainId"];
                let row=this.$parent.getSelectRow();
            
                if(row&&this.exkey["type"]=='edit'){
                    this.commonEditData["sId"]=row[sId];
                    
                }
                if(this.data[pre][sId]&&this.exkey["type"]=='add'){
                     this.commonEditData["sId"]=this.data[pre][sId];
                }
                
                this.exData[relMainId]=this.data[pre][mainId];

                console.log("sId==="+sId+"="+mainId+"="+pre+"="+relMainId+"=="+this.exkey["type"]);
                console.log(row)
                console.log(this.data[pre][mainId])
                if(!this.data[pre][mainId]){
                    if(this.exkey["type"]=="add"){
                        this.showButton=false;
                    }else if(this.exkey["type"]=="edit"){
                        this.showButton=false;
                    }else if(this.exkey["type"]=="del"){
                        this.showButton=false;
                    }
                }else{
                    this.showButton=true;
                }
            }
        },
        closeWins(){
            this.dialogVisible=false;
        },
        goSearch2(){
           
            this.$emit("goSearch","");
        },
        openWins(){
            //alert(this.exkey["type"])
            if(this.exkey["type"]=="add"||this.exkey["type"]=="edit"||this.exkey["type"]=="del"){
                let sId=this.exkey["sId"];
                let mainId=this.exkey["mainId"];
                let pre=this.exkey["pre"];
                let relMainId=this.exkey["relMainId"];
                let relMainKey=this.exkey["relMainKey"];
                let row=this.$parent.getSelectRow();
                let rows=this.$parent.getSelectRows();
                if(this.exkey["type"]=="edit"&&!row){
                    this.$message.error('请先选择一条数据');
                    return;
                }
               if(row&&this.exkey["type"]=="edit"){
                    this.commonEditData["sId"]=row[sId];
                    
                }
                this.exData[relMainId]=this.data[pre][mainId];
                
                if(relMainKey){
                    this.commonEditData.param_key=relMainKey
                    let data={};
                    data[relMainKey]=this.data[pre][mainId]
                    this.commonEditData.params=data;
                    
                }
                
                if(!this.data[pre][mainId]){
                    if(this.exkey["type"]=="add"){
                        this.showButton=false;
                    }else if(this.exkey["type"]=="edit"){
                        this.showButton=false;
                    }else if(this.exkey["type"]=="del"){
                        this.showButton=false;
                    }
                }
                //alert(this.exkey["type"]);
                if(this.exkey["type"]=="del"){
                    let ids="";
                    if(rows){
                        for(let i=0;i<rows.length;i++){
                            console.log("sid=================="+sId);
                            let id=rows[i][sId];
                            console.log("id==="+id)
                            ids=ids=="" ? id:ids+","+id;
                        }
                    }else if(row){
                        ids=row[sId];
                    }
                    console.log("row=================");
                    console.log(rows);
                    console.log(row)
                    if(!ids){
                        this.$message.error('请先选择一条数据');
                        return;
                    }
                    let msg=this.exkey['msg'];

                    let url=this.exkey.url;
                    url=!url ? "/api/view/delCommonEdit":url;
                    this.$confirm(msg, '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning'
                        }).then(() => {
                            let params={ids:ids};
                            
                            params.filename=this.exkey.filename2;
                            
                            this.$post(this.exkey.appid,url,params).then(data=>{
                                if(data[0].status=="+OK"){
                                    this.$message({
                                        message: unescape(data[0].msg),
                                         type: 'success'
                                        });
                                    this.goSearch2()
                                }else{
                                    this.$alert(unescape(data[0].msg), '错误提示', {
                                        confirmButtonText: '确定',
                                        callback: action => {}
                                    });
                                }
                            });
                        }).catch(() => {
                                    
                        });


                }
            }
           // alert(this.commonEditData.width+"=="+this.commonEditData.height)
            this.commonEditData["exData"]=this.exData;

            if(this.exkey["type"]=="add"||this.exkey["type"]=="edit"){
                this.commonEditData.isSearch='Y'
                this.$emit("openWinEdit",JSON.stringify(this.commonEditData));
            }
        },
        rowClick(data){
            this.curRow=JSON.parse(data)
        },
        rowMultiClick(data){
            this.curRows=JSON.parse(data);
        },
        selects(){
            if(this.extype=="simple"){
                if(!this.curRow){
                     this.$message.error('请选择一条数据！');
                     return;
                }
                let exkeys=this.exkey;
                /*if(exkey_.indexOf("{")==-1){
                    let exkey_="{"+this.exkey+"}";
                    exkeys=JSON.parse(exkey_);
                }*/
                    
                console.log(exkeys);
                
                let type="";
                let pre="";
                if(exkeys["type"]){
                    type=exkeys["type"]
                }
                if(exkeys["pre"]){
                    pre=exkeys["pre"]
                   // pre=pre+".";
                }
                for(let key in exkeys){
                    let key2=exkeys[key];
                    
                    /*let data={};
                    data.id=pre+key;
                    data.val=this.curRow[key2];
                    this.$emit("input",JSON.stringify(data));*/
                    this.$set(this.data[pre],key,this.curRow[key2])
                }
                if(type=="all"){

                    for(let key in this.curRow){
                       /* let data={};
                        data.id=pre+key;
                        data.val=this.curRow[key];
                        this.$emit("input",JSON.stringify(data));*/
                        this.$set(this.data[pre],key,this.curRow[key])
                    }
                }
            }else{
                let item=this.data[this.exkey];
                if(this.curRows){
                    for(let i=0;i<this.curRows.length;i++){
                       // this.data[this.exkey].push(this.curRows[i]);
                       this.$set(this.data[this.exkey],this.data[this.exkey].length,this.curRows[i]);
                        /*
                        try{
                            this.$parent.$parent.$parent.$parent.$refs.tableEdits[0].addItem(JSON.stringify(this.curRows[i]));
                        }catch(e){
                            try{
                                this.$parent.$parent.$parent.$refs.tableEdits[0].addItem(JSON.stringify(this.curRows[i]));
                            }catch(e){
                                 this.$parent.$parent.$parent.$parent.$parent.$refs.tableEdits[0].addItem(JSON.stringify(this.curRows[i]));
                            }
                        }*/
                    }
                }else{
                    this.$message.error('请至少勾选一条数据！');
                    return ;
                }
                
            }
            this.dialogVisible=false;
        }
    }
}
</script>
<style scoped>
    .el-dialog {
        display: flex;
        flex-direction: column;
        margin:0 !important;
        position:absolute;
        top:5%;
        left:5%;
        transform:translate(-90%,-90%);
        /*height:600px;*/
        max-height:calc(100% - 10px);
        max-width:calc(100% - 1px);
    }
    .el-dialog .el-dialog__body {
        flex:1;
        overflow: auto;
    }

    .input-button-select{
        padding-left:5px;
        display:inline-block;
        margin-bottom:5px;
        margin-top:5px;
    }
</style>
<style >
    .el-dialog {
        display: flex;
        flex-direction: column;
        margin:0 !important;
        position:absolute;
        top:5%;
        left:5%;
    }
    .el-dialog .el-dialog__body {
        flex:1;
        overflow: auto;
    }
    .el-dialog__body{
        padding:5px 5px !important;
    }
</style>